<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddDefaultAdmin extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $localOpenId = 'opJ6Qt_Exii0cf8xcR8neyRLTRN4';
        $devOpenId = 'owZsEwHMQLeQkKUhyhp40RDrWBrM';
        $productionOpenId = 'odpeZv-CDuVEUTGwMAmuQWSZz7MA';
        $number = '201230098019';
        if(App::environment('dev')){
            $openId = $devOpenId;
        }elseif(App::environment('production')){
            $openId = $productionOpenId;
        }elseif(App::environment('local')){
            $openId = $localOpenId;
        }else{
            $openId = null;
        }
        $user = \App\Model\User::whereOpenId($openId)->first();
        if($user){
            $user->update(['number' => $number, 'name' => '韩军']);
        }
        $admin = \App\Model\Admin::create(['number' => $number, 'password' => Hash::make('123456')]);

        //设置角色
        $owner = \App\Model\Role::create(['name' => 'owner', 'display_name' => '超级管理员']);
        $president = \App\Model\Role::create(['name' => 'president', 'display_name' => '会长']);

        //设置权限
        $login = \App\Model\Permission::create(['name' => 'login', 'display_name' => '登录后台']);
        $roleManage = \App\Model\Permission::create(['name' => 'role_manage', 'display_name' => '角色管理']);
        $adminManage = \App\Model\Permission::create(['name' => 'admin_manage', 'display_name' => '管理员管理']);
        $permissionManage = \App\Model\Permission::create(['name' => 'permission_manage', 'display_name' => '权限管理']);
        $adminDelete = \App\Model\Permission::create(['name' => 'admin_delete', 'display_name' => '删除管理员']);
        $adminRoleSetting = \App\Model\Permission::create(['name' => 'admin_role_setting', 'display_name' => '管理员角色设置']);

        $admin->attachRole($owner);
        $owner->attachPermissions([
            $login, $roleManage, $adminManage, $permissionManage, $adminDelete, $adminRoleSetting
        ]);
        $president->attachPermissions([
            $login, $roleManage, $adminManage, $adminDelete, $adminRoleSetting
        ]);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
